<template>
    <div>
        <el-table :data="props.list" style="width: 100%" size="large" border :fit="true" row-class-name="row_name"  :header-cell-style="{
            textAlign: 'center',
            height: '42px',
			background: '#e9f2fa',
        }">
            <el-table-column prop="no" label="信息编号" align="center" width="300" />
            <el-table-column label="类别" align="center" width="100" prop="cat_1_msg">
                <!-- <template #default="scope">
                    {{ scope.row.cat_1_msg[0]?.name}}
                </template> -->
            </el-table-column>
            <el-table-column prop="area_whole_msg" label="发布地区" align="center" width="100" />
            <el-table-column prop="seach_page" label="排名" align="center" width="100">
                 <template #default="scope">
                    第{{ scope.row.seach_page}}页
                </template>
            </el-table-column>
            <el-table-column prop="one_money" label="单价" align="center" width="100" />
            <el-table-column prop="seach_money" label="购买总量(点)" align="center" width="150" />
            <el-table-column prop="let_money" label="剩余点数" align="center" width="100" />
            <el-table-column label="用户" align="center" width="100">
                <template #default="scope">
                    {{ scope.row.nickname }}
                    <br>
                    {{ scope.row.number }}
                </template>
            </el-table-column>
            <el-table-column label="状态" align="center" width="100">
                <template #default="scope">
                    {{ scope.row.status === 0 ? "支付中" :  scope.row.status === 1?'进行中':'已发布'  }}
                </template>
            </el-table-column>
            <el-table-column prop="created_at" label="创建时间" align="center" width="200" />
            <el-table-column label="计划结束时间" align="center" width="200">
                <template #default="scope">
                    {{ get_time(scope.row.plain_end_time) }}
                </template>
            </el-table-column>
            <el-table-column fixed="right" label="操作" align="center" width="250">
                <template #default="scope">
                    <el-button  type="primary" size="small" @click="view_data(scope.row)" v-if="props.type=='0'">发布</el-button>
                    <el-button  type="success" size="small" @click="view_data(scope.row)" v-if="scope.row.status=='1'&&props.type!='0'">审核</el-button>
                    <!-- <el-button  type="primary" size="small" @click="goDetail(scope.row)"  v-if="props.type!='0'">查看</el-button> -->
                    <el-button  type="success" size="small" @click="view_data(scope.row)"  v-if="scope.row.status=='5'&&props.type!='0'">复制</el-button>

                    <!-- <el-button link type="primary" size="small" @click="handleEdit(scope.$index, scope.row)">复制</el-button> -->
                </template>
            </el-table-column>
        </el-table>
        <el-dialog v-model="dialogVisible" width="30%" @close="close">
            <div v-html="ruleForm.content" class="dialog_content">

            </div>
            <div class="copy">
                <el-button type="primary" @click="copy(ruleForm.content)">复制文本</el-button>
            </div>
            <div class="link_list" v-if="img_list.length>0||video_list.length>0">
                <span>图片/视频：</span>
                <ul>
                    <li v-for="(item, index) in img_list" :key="index">
                        <img :src="item.file?.[0]" alt="">
                        <a @click="downloadFile(item.file?.[0], item.id)">下载</a>
                    </li>
                    <li v-for="(item,index) in video_list" :key="index">
                        <img :src="item.imgsrc" alt="">
                        <!-- <a @click="downloadFile(item.file?.[0],item.id)">下载</a> -->
                        <a  @click="daonload(item.file?.[0],item.id)">下载视频</a>
                    </li>
                </ul>
            </div>
            <div class="content_dialog">
                <h3>基础信息</h3>
                <div class="item">
                    <span>用户</span>
                    <div class="content">
                        {{ ruleForm.nickname }}
                        <el-button type="primary" @click="copy(ruleForm.nickname)" class="copy_button">复制</el-button>
                    </div>
                </div>
                <div class="item">
                    <span>创建时间</span>
                    <div class="content">
                        {{ ruleForm.created_at }}
                    </div>
                </div>
                <div class="item">
                    <span>第三方ID</span>
                    <div class="content">
                        <el-input v-model="ruleForm.seach_three_id"></el-input>
                    </div>
                </div>
                <div class="item">
                    <span>发布地区</span>
                    <div class="content">
                        {{ ruleForm.area_msg }}
                    </div>
                </div>
                <div class="item">
                    <span>备注</span>
                    <div class="content">
                        <el-input v-model="ruleForm.textarea" type="textarea" placeholder="请输入" resize="none" />
                        <!-- <div class="copy_button_con">
                            <el-button type="primary" @click="copy(ruleForm.textarea)"
                                class="copy_button_textarea">复制</el-button>
                        </div> -->
                    </div>
                </div>
                <div class="submit_button">
                    <el-button type="info" class="button" @click="dispose_list(4)">驳回</el-button>
                    <el-button type="primary" class="button" @click="dispose_list(5)">通过并发布</el-button>
                </div>
            </div>
        </el-dialog>
    </div>
</template>
  
<script lang="ts" setup>
import axios from 'axios';
import { dispose_data } from "@/request/api/search.ts"
import useClipboard from 'vue-clipboard3'
import { get_time } from "@/utils/time"
import router from "@/router";
import { defineProps, reactive, ref } from "vue"
import { reload } from "@/utils/flushed"
let dialogVisible = ref(false)
// 图片数据列表
let img_list: any = ref([])
// 视频列表
let video_list:any = ref([])
const props = defineProps(["height", "list","type"])
const handleEdit = (index: number, row: any) => {
    copy(row.content)
}
const goDetail = (row: any)=>{
    //  router.push(`/detail?info=${JSON.stringify(row)}`)
    let obj= JSON.parse(JSON.stringify(row));
    router.push({name:"taskdetail",state:{
        obj
    }})
    
}
const view_data = (row: any) => {
    ruleForm.id = row.id
    ruleForm.content = row.content
    ruleForm.no = row.no
    ruleForm.cat_2_msg = row.cat_2_msg
    ruleForm.area_msg = row.area_whole_msg
    ruleForm.style = row.style === 1 ? "普通信息" : "vip信息"
    ruleForm.refresh_times = row.refresh_times
    ruleForm.is_pay = row.is_pay === 1 ? "是" : "不是"
    ruleForm.nickname = row.nickname + "/" + row.admin_id + "/" + row.number
    // ruleForm.number = row.number
    ruleForm.plain_end_time = get_time(row.plain_end_time)
    ruleForm.created_at = row.created_at
    ruleForm.status = row.status
    ruleForm.admin_nickname = row.admin_nickname
    ruleForm.done_time = row.done_time
    axios
        .post('http://api.customer.form.bailingkeji.com/admin/record/question/info', {
            filling_id: row.rpc_id,
            type: 7,
            // other_type : 6
        })
        .then(({ data }) => {
            console.log(data, "response");
            img_list.value = data.data
            //   inputList.value = response.data.data.record;
            //   loading.value = false;
        });
    axios
        .post('http://api.customer.form.bailingkeji.com/admin/record/question/info', {
            filling_id: row.rpc_id,
            type: 8,
            other_type: 6
        })
        .then(({ data }) => {
            console.log(data, "response111");
            video_list.value = data.data
            video_list.value.forEach(async(item:any) => {
                item.imgsrc = await getVideoBase64(item.file?.[0])
            });
        });
    dialogVisible.value = true
    console.log(ruleForm.content, " ruleForm.content");
    dialogVisible.value = true
    console.log(ruleForm.content, " ruleForm.content");
    // router.push(`/detail?info=${JSON.stringify(row)}`)

}
let ruleForm = reactive({
    id : "",
    no: "",
    cat_2_msg: "",
    area_msg: "",
    style: "",
    refresh_times: "",
    is_pay: "",
    nickname: "",
    number: "",
    plain_end_time: "",
    created_at: "",
    status: "",
    admin_nickname: "",
    done_time: "",
    content: "",
    textarea: '',
    seach_three_id:''
})
const close = ()=>{
    ruleForm.textarea = ''
    img_list.value = []
    video_list.value = []
}
// 点击复制
// 使用插件
const { toClipboard } = useClipboard()
const copy = async (msg: any) => {
    try {
        // 复制
        await toClipboard(msg)
        ElMessage({
            message: '复制成功',
            type: 'success',
        })
        // 复制成功
    } catch (e) {
        // 复制失败
        ElMessage.error("复制失败")
    }
}
const dispose_list =async (style:any)=>{
    if(!ruleForm.seach_three_id){
        ElMessage({
          message: '请输入第三方ID',
          type: 'error',
        });
        return 
    }
    let data:any = await dispose_data({id:ruleForm.id,style,keyword:ruleForm.textarea,seach_three_id:ruleForm.seach_three_id})
    if (data.code != 200) return ElMessage({
            message: data.message,
            type: 'error',
          });
        ElMessage({
          message: '操作成功',
          type: 'success',
        });
        reload()
}
function downloadFile(url: string, filename?: string) {
    fetch(url, {
        headers: new Headers({
            Origin: location.origin,
        }),
        mode: 'cors',
    })
        .then(res => {
            if (res.status == 200) {
                // 返回的.blob()为promise，然后生成了blob对象，此方法获得的blob对象包含了数据类型，十分方便
                return res.blob()
            }

            throw new Error(`status: ${response.status}.`)

        })
        .then(blob => {
            download(blob, filename)
        })
}

function download(blob: Blob, filename?: string) {
    const a: any = document.createElement('a')
    a.download = filename
    const blobUrl = URL.createObjectURL(blob)
    a.href = blobUrl
    document.body.appendChild(a)
    a.click()
    a.remove()
    URL.revokeObjectURL(blobUrl)
}
// 视频图片处理
function getVideoBase64(url: any) {
    return new Promise(function (resolve, reject) {
        let dataURL = '';
        let video = document.createElement("video");
        video.setAttribute('crossOrigin', 'anonymous');//处理跨域
        video.setAttribute('src', url);
        video.setAttribute('width', 400);
        video.setAttribute('height', 240);
        video.setAttribute('preload', 'auto');
        video.addEventListener('loadeddata', function () {
            let canvas = document.createElement("canvas"),
                width = video.width, //canvas的尺寸和图片一样
                height = video.height;
            canvas.width = width;
            canvas.height = height;
            canvas.getContext("2d").drawImage(video, 0, 0, width, height); //绘制canvas
            dataURL = canvas.toDataURL('image/jpeg'); //转换为base64
            resolve(dataURL);
        });
    })
}
// 下载函数
function daonload(url:any,name:any){
	fetch(url)
	.then(res => res.blob())
	.then(blob => {
		const a = document.createElement("a");
		const objectUrl = window.URL.createObjectURL(blob);
		a.download = name;
		a.href = objectUrl;
		a.click();
		window.URL.revokeObjectURL(objectUrl);
		a.remove();
	})
}
</script>
<style lang="scss" scoped>
:deep(.cell){
    color:#5e6d82 ;
    font-size: 13px;
}
:deep(thead){
    .cell{
        color:#1f2f3d ;
        font-size: 14px;
        font-weight: 650;
    }
}

:deep(.el-dialog) {
    margin-top: 20px;
}

.dialog_content {
    white-space: pre-wrap;
    @include font(#333, 16, 36);
}

.copy {
    text-align: center;
    margin: 10px 0;
}

.content_dialog {
    h3 {
        @include font(#333, 24, 36);
        font-weight: 550;
    }

    .item {
        display: flex;
        margin-top: 20px;

        // align-items: center;
        span {
            @include font(#333, 16, 20);
            font-weight: 550;
            display: block;
            width: 150px;
        }

        .copy_button {
            margin-left: 30px;
            width: 100px;
            height: 30px;
        }

        .content {
            flex: 1;
        }

        .copy_button_con {
            text-align: center;

            .copy_button_textarea {
                width: 100px;
                height: 30px;
                margin-top: 20px;
            }
        }
    }

    .submit_button {
        padding-top: 20px;
        border-top: 1px solid rgb(242, 242, 242);
        text-align: center;
        .button{
            width: 100px;
        }
    }
}

:deep(.el-textarea__inner) {
    height: 100px;

}
:deep(.row_name){
   height: 42px !important;
}
:deep(.el-table__cell){
    padding: 0;
}
:deep(.el-textarea) {
    height: auto !important;
}

:deep(.row_name) {
    height: 42px !important;
}

:deep(.el-table__cell) {
    padding: 0;
}

.copy_button_con_twe {
    margin-bottom: 20px;
}

.link_list {
    display: flex;

    span {
        display: block;
        width: 90px;
    }

    ul {
        display: flex;
        flex-wrap: wrap;

        li {
            text-align: center;

            img {
                display: block;
                width: 100px;
                height: 100px;
                object-fit: cover;
            }

            a {
                cursor: pointer;
                margin-top: 5px;
                display: block;
            }

            margin-right: 10px;

            &:last-child {
                margin-right: 0;
            }
        }
    }
}
</style>
  